@echo off
call base.cmd
setlocal
set file=%1
IF %ERRORLEVEL% EQU 1 GOTO :EOF

set log=%2
IF %ERRORLEVEL% EQU 1 SET log=database.log

@rem Rename existing pgpass.conf file
SET confFile=pgpass.conf
SET conf="%APPDATA%\postgresql\%confFile%"
SET oldConfFile=pgpass.conf_
SET oldConf="%APPDATA%\postgresql\%oldConfFile%"
IF EXIST %conf% RENAME %conf% %oldConfFile%

@rem Write new conf
echo %host%:%port%:%dbname%:%user%:%password%> %conf%

@rem Execute script
psql -f %file% -d %dbname% -h %host% -p %port% -U %user% >> log.log

@rem Restore old file
del /Q %conf%
IF EXIST %oldConf% RENAME %oldConf% %confFile%
